// 字体包
import '@/assets/font-ttf/lenovo-bold-normal.js';
import jsPDF from 'jspdf';
import 'jspdf-autotable';

export const DownPDF = (dataSource: any) => {
  const doc = new jsPDF();
  const PAGE_MARGIN = 10;
  // 页面宽高
  const width = doc.internal.pageSize.getWidth();
  const height = doc.internal.pageSize.getHeight();
  // 内容最大宽高
  const pdfCentWidth = width - PAGE_MARGIN * 2;
  const pdfCentHeight = height - PAGE_MARGIN * 2;
  let startY = PAGE_MARGIN;

  // splitTextToSize方法将文本分割为适合页面宽度的行
  // const splitText = doc.splitTextToSize(text, pdfCentWidth - 80);

  // 设置title
  const title = dataSource.title;
  doc.setFont('lenovo-bold', 'normal');
  doc.setFontSize(18);
  doc.text(title, PAGE_MARGIN, startY);
  startY += PAGE_MARGIN; // 更新startY

  // 设置主体信息
  dataSource.textData.forEach((item, index) => {
    let text = `${item.name} ${item.value}`;
    doc.setFont('lenovo-bold', 'normal');
    doc.setFontSize(12);
    const lineHeight = PAGE_MARGIN; // 基于字体大小的行高估算
    doc.text(text, PAGE_MARGIN, startY);
    startY += lineHeight; // 每行文本后更新startY
  });


  // 表格组件
  // data数据处理
  const flattenDataWithColumns = (data, column, depth = 0) => {
    let resultList = [];

    for (const item of data) {
      // 根据层级计算空格数量并添加到varName前
      const indentation = '  '.repeat(depth);
      const formattedVarName = indentation + item.varName;
      const other = column.map((columns) => {
        const str = columns.prop === 'varName' ? formattedVarName : item[columns.prop];
        return str;
      });
      resultList.push(other);

      // 递归处理子元素，增加深度计数
      if (item.children && item.children.length > 0) {
        resultList = resultList.concat(flattenDataWithColumns(item.children, column, depth + 1));
      }
    }
    return resultList;
  }

  // 表格数据处理
  const tableList = (column: any, data: any, startY: number) => {
    const headers = column.map((item) => item.label);
    const body = flattenDataWithColumns(data, column);
    // 设置表头样式
    jsPDF.autoTableSetDefaults(
      {
        headStyles: { fillColor: [20,171,151] }, // Green
      }
    );
    doc.autoTable({
      head: [headers],
      body: body,
      startY: startY,
      pageBreak: 'auto', // 自动分页
      rowPageBreak: 'auto', // 行分页
      bodyStyles: { valign: 'top' },
      styles: {
        font: 'lenovo-bold', // 设置表格字体
        fontStyle: 'normal', // 设置表格字体样式
      },
    });
    // 表格渲染结束的位置
    const tableY = doc.lastAutoTable.finalY;
    startY = tableY + PAGE_MARGIN;
    return tableY;
  };

  // 添加表格，并在需要时分页
  dataSource.docSource.forEach((item) => {
    const tableText = `${item.title}`;
    doc.setFont('lenovo-bold', 'normal');
    doc.setFontSize(14);
    const tableHeaderTextHeight = 8; // 假设标题行高
    doc.text(tableText, PAGE_MARGIN, startY);
    startY += tableHeaderTextHeight; // 更新startY以容纳标题

    const startTableY = startY; // 记录表格起始Y坐标
    tableList(item.column, item.data, startY);
    const tableEndY = doc.lastAutoTable.finalY; // 获取表格结束的Y坐标
    startY = Math.max(startY, tableEndY) + PAGE_MARGIN; // 确保startY在表格下方
  });
  // 保存文件
  doc.save(`${dataSource.docName}.pdf`);

  // return doc;
};
